package com.linewell.serviceImpl;

import com.linewell.annotation.SystemLogAop;
import com.linewell.dao.BookDao;
import com.linewell.pojo.Book;
import com.linewell.service.BookService;
import com.linewell.util.SortModelUtils;
import com.linewell.util.query.QueryBuilder;
import com.linewell.util.query.QueryEntity;
import com.linewell.util.query.QueryModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: demo
 * @description:
 * @author: hyuhuang@linewell.com
 * @since:2018/4/26
 */
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    BookDao bookDao;

    @Override
    public void insertBook(Book book) {
        bookDao.insertBook(book,"test","tesets");
    }

    @Override
    public void deleteBook(String l) {
        Query query = Query.query(Criteria.where("id").is(l));
        bookDao.deletes(query);
    }

    @Override
    public void update(String id, String updateValue) {
        Map map = new HashMap();
        map.put("reader", updateValue);
        bookDao.updateBook(id, map);
    }
    @Override
    @SystemLogAop(value = "查询数据")
    public void findBooks(int page, int size) {
        Pageable pageable = new PageRequest(page,size);
        bookDao.findBooks(pageable);
    }

    @Override
    public void findBookByCon(Map<String, QueryEntity> map, Integer[] sort, String[] fields, QueryModel model) {
        /*QueryBuilder builder = new QueryBuilder(map,model);
        Criteria ca = builder.queryCriteria();
        Sort s = SortModelUtils.getMongodbSort(sort, fields,Book.class);
        Query query = new Query(ca);
        query.with(s);*/
        Query query =getQuery(map,sort,fields,model);
        bookDao.findBookByCon(query);
    }

    @Override
    public void findBookSort(Map<String, QueryEntity> map, int page, int size, Integer[] sort, String[] fields, QueryModel model) {
        Query query =getQuery(map,sort,fields,model);
        Pageable pageable = new PageRequest(page, size);
        bookDao.findBookSort(query,pageable);
    }

    public static Query getQuery(Map<String, QueryEntity> map, Integer[] sort, String[] fields, QueryModel model){
        QueryBuilder builder = new QueryBuilder(map,model);
        Criteria ca = builder.queryCriteria();
        Sort s = SortModelUtils.getMongodbSort(sort, fields,Book.class);
        Query query = new Query(ca);
        query.with(s);
        return query;
    }

    @Override
    public void countBook(Map<String, QueryEntity> map, QueryModel model) {
        QueryBuilder queryBuilder = new QueryBuilder(map,model);
        Criteria ca = queryBuilder.queryCriteria();
        Query query = new Query(ca);
        bookDao.countBook(query);
    }
}

